package servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import bdd.beans.Enseignant;
import bdd.beans.Indisponibilites;
import bdddaosimpl.DAOIndisponibiliteImpl;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author anwarend
 */
public class FicheVoeuxEnseignantServlet extends HttpServlet {

   
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    DAOIndisponibiliteImpl e = new DAOIndisponibiliteImpl();
    List<Indisponibilites> listeFicheVoeux = e.findByEnseignant(1);/*à remplacer par e.findByEnseignant(SESSION_NUMENSEIGNANT);*/
    request.setAttribute("listeFicheVoeux", listeFicheVoeux);
    // On re-route l'appel vers la page d'accueil JSP
    gotoPage("/fiche_voeux_enseignant.jsp", request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        DAOIndisponibiliteImpl e = new DAOIndisponibiliteImpl();
        if(request.getParameter("supprimer")!=null)
        {
            Indisponibilites o = new Indisponibilites(Integer.parseInt(request.getParameter("numindisponibilite")),null,1);
            e.delete(o);
        }
        if(request.getParameter("valider")!=null)
        {
            int h= Integer.parseInt(request.getParameter("horaire"));
            int j= Integer.parseInt(request.getParameter("jours"));
            int d = Integer.parseInt(request.getParameter("degre"));
            int nE= 1;/*à remplacer par SESSION_NUMENSEIGNANT*/
            List<Indisponibilites> l = e.findByHoraireJoursEnseignant(h, j, nE);
            request.setAttribute("rs","0");
            if(l.isEmpty())
            {
                Enseignant en = new Enseignant(1/*à remplacer par pst.setInt(4, SESSION_NUMENSEIGNANT)*/);
                Indisponibilites o = new Indisponibilites(en,h,j,d);
                e.save(o);
                request.setAttribute("rs","1");
            }
        }
        List<Indisponibilites> listeFicheVoeux = e.findByEnseignant(1);/*à remplacer par e.findByEnseignant(SESSION_NUMENSEIGNANT);*/
        request.setAttribute("listeFicheVoeux", listeFicheVoeux);
        // On re-route l'appel vers la page d'accueil JSP
        gotoPage("/fiche_voeux_enseignant.jsp", request, response);
        
    }


     private void gotoPage(String adress, HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(adress);
    dispatcher.forward(request,response);
  }
}
